package com.atguigu.order.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.order.entity.TOrder;
import com.atguigu.order.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/order/torDer")
@CrossOrigin
@Slf4j
public class TOrderController{
    @Autowired
    private TOrderService orderService;

    //添加订单
    @PostMapping("createOrder/{courseId}")
    public Result saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        String orderId = orderService.CreateOrders(courseId, request);
        return Result.ok().data("orderId", orderId);
    }

    //根据订单号查询订单信息
    @PostMapping("getOrderInfo/{orderId}")
    public Result getOrderInfo(@PathVariable String orderId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        TOrder order = orderService.getOne(wrapper);
        return Result.ok().data("orderInfo", order);
    }
    //根据课程id和用户id查询订单表中的订单状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId, @PathVariable String memberId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberId);
        wrapper.eq("status", 1);
        log.info("我开始执行了");
        int count = orderService.count(wrapper);
        return count > 0;
    }
    //   /order/torDer/isBuyCourse/{courseId}/{memberId}
}

